fs = require 'fs'
path = require 'path'
_ = require 'underscore'
request = require 'request'
requireTree = require 'require-tree'

mods = requireTree '../mods'
config = require '../config'

Importer = require '../helpers/importer'

if config.env == 'development'
  compsFile = path.join __dirname, '../comps.json'
  
recordImportFiles = (template, infos) ->
  comps = JSON.parse fs.readFileSync compsFile
  comps[template] = infos
  keys = _.keys(comps).sort()
  tmp = {}
  _.each keys, (key) ->
    tmp[key] = comps[key]
    return
  fs.writeFileSync compsFile, JSON.stringify(tmp, null, 2)

addImporter = (req, res, next) ->
  options = 
    urlPrefix : config.component.urlPrefix
  if config.env != 'development'
    options.merge = require '../merge'
    options.crc32Infos = require '../crc32'
  importer = new Importer options
  res.locals.importer = importer
  importer.on 'beforeExport', recordImportFiles if config.env == 'development'
  next()

routes = [
  # 图片验证码
  {
    route: ['/captcha.jpg']
    type: 'GET'
    middleware: []
    handler: mods.common.captcha_ctrl.getCaptcha
  }
  {
    route: ['/query_tzqx_rate']
    type: 'GET'
    middleware: []
    handler: mods.otc_tzqx_rate.otc_tzqx_rate_ctrl.query
  }
  {
    route: ['/save_tzqx_rate']
    type: 'POST'
    middleware: []
    handler: mods.otc_tzqx_rate.otc_tzqx_rate_ctrl.save
  }
  {
    route: ['/remove_tzqx_rate']
    type: 'GET'
    middleware: []
    handler: mods.otc_tzqx_rate.otc_tzqx_rate_ctrl.remove
  }
  {
    route: ['/query_sypz_date']
    type: 'GET'
    middleware: []
    handler: mods.otc_tzqx_rate.otc_tzqx_rate_ctrl.query
  }
  # 保存字典
  {
    route: ['/dict_save']
    type: 'POST'
    # middleware: [mods.common.auth_ctrl.checkIsLoginBack, mods.common.auth_ctrl.checkAuthBack]
    handler: mods.dict.dict_ctrl.save
  }
  # 删除字典
  {
    route: ['/dict_remove']
    type: 'POST'
    # middleware: [mods.common.auth_ctrl.checkIsLoginBack, mods.common.auth_ctrl.checkAuthBack]
    handler: mods.dict.dict_ctrl.remove
  }
  # 查询字典 - 分页查询
  {
    route: ['/dict_query']
    type: 'POST'
    middleware:[]
    # middleware: [mods.common.auth_ctrl.checkIsLoginBack, mods.common.auth_ctrl.checkAuthBack]
    handler: mods.dict.dict_ctrl.query
  }
  {
    route: ['/department_save']
    type: 'POST'
    # middleware: [mods.common.auth_ctrl.checkIsLoginBack, mods.common.auth_ctrl.checkAuthBack]
    handler: mods.department.department_ctrl.save
  }
  # 删除字典
  {
    route: ['/department_remove']
    type: 'POST'
    # middleware: [mods.common.auth_ctrl.checkIsLoginBack, mods.common.auth_ctrl.checkAuthBack]
    handler: mods.department.department_ctrl.remove
  }
  # 查询字典 - 分页查询
  {
    route: ['/department_query']
    type: 'POST'
    middleware:[]
    # middleware: [mods.common.auth_ctrl.checkIsLoginBack, mods.common.auth_ctrl.checkAuthBack]
    handler: mods.department.department_ctrl.query
  }
  # 上传文件
  {
    route: ['/file_upload/:comp_id', '/file_upload/:comp_id/:img_url_type']
    type: 'POST'
    # middleware: [mods.common.auth_ctrl.checkIsLoginBack, mods.common.auth_ctrl.checkAuthBack]
    handler: mods.file.file_upload.upload
  }
]

module.exports = routes